#############################################################################
## compiler and related, set or default

# CC
CC := $(CROSS_COMPILER)gcc

# CXX
CXX := $(CROSS_COMPILER)g++

# # AR
AR ?= $(CROSS_COMPILER)ar

# # RANLIB
RANLIB ?= $(CROSS_COMPILER)ranlib

# LINKER
LINKER ?= $(CXX)

# STRIP
STRIP := $(CROSS_COMPILER)strip

QT_MOC := $(QT_MOC)


#############################################################################
## CFLAGS, CXXFLAGS and LDFLAGS
#
BUILD_ROOT ?= .
QT_ROOT ?= .
PATH2INC ?= $(BUILD_ROOT)/include
CFLAGS += -I$(PATH2INC)
CFLAGS += -DUNICODE
CFLAGS += -Wall -Wno-unused-parameter
# deps
CFLAGS += -Wp,-MD,$@.d
CFLAGS += -fpic

CXXFLAGS += -I$(PATH2INC)
CXXFLAGS += -Wall -Wno-unused-parameter
CXXFLAGS += -DUNICODE
# deps
CXXFLAGS += -Wp,-MD,$@.d
ifneq ($(GCCONW),)
  CXXFLAGS += -I$(QT_ROOT)/include
else
  ifneq ($(QT4),)
    CXXFLAGS += -I/opt/qt-4.8.5-o/include
  endif
endif
CXXFLAGS += -DID_QT_VERSION=$(ID_QT_VERSION)

ifneq ($(PIMETHOD_DEBUG),)
  CXXFLAGS += -DPIMETHOD_DEBUG=$(PIMETHOD_DEBUG)
endif

ifneq ($(MINGW32),)
CXXFLAGS += -DMINGW32=1
endif
# CXXFLAGS_MOC += -Iinclude
CXXFLAGS_MOC += -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB


# ##
# ARFLAGS = crv

## LDFLAGS
ifneq ($(MINGW32),)
  ifneq ($(QT4),)
    LDFLAGS += -L$(BUILD_ROOT)/lib
    LDFLAGS += -lQtCore4
    LDFLAGS += -lQtGui4
  else
    LDFLAGS += -L$(QT_ROOT)/lib
    LDFLAGS += -lQt5Core
    LDFLAGS += -lQt5Gui
    LDFLAGS += -lQt5Widgets
  endif # ifneq ($(QT4),)
  LDEXTRA1 += $(BUILD_ROOT)/lib/XDLL.lib
  LDEXTRA2 += $(BUILD_ROOT)/lib/libzinnia.lib
else
  LDFLAGS += -L/opt/qt-4.8.5-o/lib
  LDFLAGS += -L$(BUILD_ROOT)/lib
  LDFLAGS += -lQtCore
  LDFLAGS += -lQtGui
endif
LDFLAGS += --static


#############################################################################
## VPATH
VPATH += $(BUILD_ROOT)/include
VPATH += $(BUILD_ROOT)/pattern
VPATH += $(BUILD_ROOT)/xyb
VPATH += $(BUILD_ROOT)/xyb-demo
VPATH += $(BUILD_ROOT)/zinnia
VPATH += $(BUILD_ROOT)/zinnia-demo


#############################################################################
## out
OUT ?= .

# out
VPATH += $(OUT)


## TARGET
#
ifneq ($(MINGW32),)
  TARGET1 ?= $(OUT)/xyb_handwriting_demo.exe
  TARGET2 ?= $(OUT)/zinnia_handwriting_demo.exe
else
  TARGET1 ?= $(OUT)/xyb_handwriting_demo
  TARGET2 ?= $(OUT)/zinnia_handwriting_demo
endif

SUFFIX_DLIB ?= .dll
SUFFIX_SLIB ?= .dll.a
SUFFIX_SLIB ?= .a

TARGET_LIB1 := $(OUT)/libpim$(SUFFIX_DLIB)
# TARGET_LIB2 := $(OUT)/libpim.a
LDFLAGS_D := -shared -fPIC
LDFLAGS_S := --static

#
TARGET += $(TARGET1)
TARGET += $(TARGET2)
TARGET += $(TARGET_LIB1)
all: .ready $(TARGET)
	@echo "all done"


#############################################################################
## OBJS
#
SUFFIX_OBJ ?= .o

#
OBJS_COMMON += $(OUT)/inputmethod_manager$(SUFFIX_OBJ)
OBJS_COMMON += $(OUT)/inputmethod$(SUFFIX_OBJ)
OBJS_COMMON += $(OUT)/moc_inputmethod$(SUFFIX_OBJ)

#
OBJS1 += $(OBJS_COMMON)
OBJS1 += $(OUT)/main$(SUFFIX_OBJ)
OBJS1 += $(OUT)/xyb_handwriting_manager$(SUFFIX_OBJ)
OBJS1 += $(OUT)/xyb_handwriting$(SUFFIX_OBJ)
OBJS1 += $(OUT)/xyb_handwriting_demo$(SUFFIX_OBJ)
# # moc_
OBJS1 += $(OUT)/moc_xyb_handwriting$(SUFFIX_OBJ)
OBJS1 += $(OUT)/moc_xyb_handwriting_demo$(SUFFIX_OBJ)

#
OBJS2 += $(OBJS_COMMON)
OBJS2 += $(OUT)/zinnia_main$(SUFFIX_OBJ)
OBJS2 += $(OUT)/zinnia_handwriting_manager$(SUFFIX_OBJ)
OBJS2 += $(OUT)/zinnia_handwriting$(SUFFIX_OBJ)
OBJS2 += $(OUT)/zinnia_handwriting_demo$(SUFFIX_OBJ)
# # moc_
OBJS2 += $(OUT)/moc_zinnia_handwriting$(SUFFIX_OBJ)
OBJS2 += $(OUT)/moc_zinnia_handwriting_demo$(SUFFIX_OBJ)

OBJS_LIBS1 += $(OBJS_COMMON)
OBJS_LIBS1 += $(OUT)/xyb_handwriting_manager$(SUFFIX_OBJ)
OBJS_LIBS1 += $(OUT)/xyb_handwriting$(SUFFIX_OBJ)
OBJS_LIBS1 += $(OUT)/moc_xyb_handwriting$(SUFFIX_OBJ)
OBJS_LIBS1 += $(OUT)/zinnia_handwriting_manager$(SUFFIX_OBJ)
OBJS_LIBS1 += $(OUT)/zinnia_handwriting$(SUFFIX_OBJ)
OBJS_LIBS1 += $(OUT)/moc_zinnia_handwriting$(SUFFIX_OBJ)


#############################################################################
## build
#
.ready:
	@mkdir -p $(OUT)

#
$(TARGET1): $(OBJS1)
	$(LINKER) -o $@ $^ $(LDFLAGS) $(LDEXTRA1)
ifeq ($(MINGW32),)
	$(STRIP) $@
endif

$(TARGET2): $(OBJS2)
	$(LINKER) -o $@ $^ $(LDFLAGS) $(LDEXTRA2)
ifeq ($(MINGW32),)
	$(STRIP) $@
endif

$(OUT)/%.o: %.c
	$(CC) -c -o $@ $< $(CFLAGS)
$(OUT)/%.o: %.cpp
	$(CXX) -c -o $@ $< $(CXXFLAGS)

# shared lib and static lib
$(TARGET_LIB1): $(OBJS_LIBS1)
	$(LINKER) -o $@ $^ \
		-Wl,--output-def,$(OUT)/libpim.def,--out-implib,$(OUT)/libpim$(SUFFIX_SLIB) \
		$(LDFLAGS) $(LDFLAGS_D) $(LDEXTRA1) $(LDEXTRA2)

# $(OUT)/moc_%.cpp: include/%.h
$(OUT)/moc_%.cpp: %.h
	$(QT_MOC) -o $@ $< $(CXXFLAGS_MOC)


#############################################################################
## deps
eac_files := $(foreach f,$(OBJS),$(OUT)/.$(f).d) # each
dep_files := $(wildcard $(eac_files)) # depend
ifneq ($(dep_files),)
  include $(dep_files)
endif

# common


#############################################################################
## clean
clean:
	@rm -fv $(OBJS) $(TARGET)

distclean: clean
	@rm -fv $(OUT)/*.o.d
ifneq ($(OUT), .)
	@rmdir $(OUT)/
endif

# UPDATE_COMMON
UPDATE_COMMON:
	cd $(PWD)/common; ./update.gitrepo.sh $(PWD)/common

## install

install: .ready $(TARGET)
ifeq ($(PREFIX),)
	$(error "install: should provide a NOT-NULL PREFIX")
endif
	install -d $(PREFIX)/bin
	install -m 775 $(TARGET1) $(PREFIX)/bin
	install -m 775 $(TARGET2) $(PREFIX)/bin

##
.PHONY: all install clean distclean .ready


## ========================================================================= #
#
#        Filename:  Makefile
#
#     Description:
#
# - Mode: 644
# - NOTE. NOT use "dos"
# - Updated: TODO
#
#         Version:  1.0.0
#         Created:  2015-04-16 13:38:17
#        Revision:	1.0.0
#
## ========================================================================= #
